/* eslint-disable */
import { createRouter, createWebHistory,createWebHashHistory } from "vue-router";
import Login from "@/view/LoginPage.vue";
import Home from "@/view/HomePage.vue";
import Cookies from "js-cookie";

const routes = [
  {
    path: "/",
    redirect: "/home",
  },
  {
    path: "/login",
    name: "login",
    component: Login,
  },
  {
    path: "/home",
    name: "Home",
    component: Home,
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  if (to.path === "/login") return next();
  const tokenStr = Cookies.get("token");
  if (!tokenStr) {
    console.log("跳转登录页面");
    return next("/login");
  } else {
    console.log("已经登录,跳转首页");
    if (to.path === "/home") return next();
    return next("/home");
  }
  // next();
});

export default router;
